package com.ship.control.impl;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.ship.common.CommonUtil;
import com.ship.control.AuthorizationCredit;
import com.ship.entity.User;
import com.ship.enume.LoginMessage;

/**
 * 用户登录
 * @author wqf
 * @version  Nov 25, 2011 9:26:07 AM
 */
public class LoginController extends SimpleFormController {
	
	public LoginController(){
		this.setCommandClass(User.class);
	}
	
	protected ModelAndView onSubmit(HttpServletRequest request,HttpServletResponse response,Object command,BindException errors)
    throws Exception{
   	
		User people=(User)command;				
		LoginMessage loginMessage=this.authorizationCredit.login(request, people.getSign(), people.getPass());
		if(loginMessage==LoginMessage.NOFOUND){
			request.setAttribute("message", "对不起，系统找不到此用户，请确认输入用户名是否正确！");
			return this.showForm(request, errors, this.getFormView());
		}
		
		if(loginMessage==LoginMessage.ERROR){
			request.setAttribute("message", "对不起，用户名或密码输入错误，请重新输入！");
			return this.showForm(request, errors, this.getFormView());
		}
		
		if(loginMessage==LoginMessage.LIMIT){
			request.setAttribute("message", "对不起，此账号被限制登录！");
			return this.showForm(request, errors, this.getFormView());
		}
	
		CommonUtil.redirectURL(request, response, this.homeUrl);
		return null;
		
	}
	
	private String homeUrl;
	private AuthorizationCredit authorizationCredit;

	public void setAuthorizationCredit(AuthorizationCredit authorizationCredit) {
		this.authorizationCredit = authorizationCredit;
	}

	public void setHomeUrl(String homeUrl) {
		this.homeUrl = homeUrl;
	}
	


}
